import React, { useState, useEffect } from 'react';
import Head from 'next/head'
import { Row, Col, List, Breadcrumb } from 'antd'
import { CalendarOutlined, FireOutlined, FolderOutlined } from '@ant-design/icons'
import Author from '../components/Author'
import Advert from '../components/Advert'
import Footer from '../components/Footer'

import Header from '../components/Header'
import axios from 'axios'
import servicePath from '../config/apiUrl';
import Link from 'next/link'

const MyList = (props) => {
  // console.log(props)
  const [mylist, setMylist] = useState(props.data)

  useEffect(() => {
    setMylist(props.data)
  })

  return (
    <div className="container">
      <Head>
        <title>{props.url.query.title}</title>
      </Head>
      <Header />
      
      <Row className="comm-main" type="flex" justify="center">
        <Col className="comm-left" xs={24} sm={24} md={16} lg={14} xl={12}>

          <div className="bread-div">
            <Breadcrumb className="bread">
              <Breadcrumb.Item><a href="/">首页</a></Breadcrumb.Item>
              <Breadcrumb.Item>{props.url.query.title}</Breadcrumb.Item>
            </Breadcrumb>
          </div>

          <List 
            itemLayout="vertical"
            dataSource={mylist}
            renderItem={(item) => {
              return (
                <List.Item>
                  <div className="list-title">
                    <Link href={{pathname:'/detailed',query:{id:item.id}}}>
                      <a>{item.title}</a>
                    </Link>
                    
                  </div>
                  <div className='list-icon'>
                    <span>
                      <CalendarOutlined />
                      &nbsp; {item.addTime}
                    </span>
                    <span>
                      <FolderOutlined />
                      &nbsp; {item.typeName}
                    </span>
                    <span>
                      <FireOutlined />
                      &nbsp; {item.view_count}人
                    </span>
                  </div>
                  <div className="list-context">{item.introduce}</div>
                </List.Item>
              )
            }}
          />
        </Col>
        <Col className="comm-right" xs={0} sm={0} md={8} lg={4} xl={4}>
          <Author />
          
        </Col>
      </Row>
      
      <Footer />
    </div>
  )
}

MyList.getInitialProps = async (context) => {
  let id = context.query.id
  
  const promise = new Promise((resolve) => {
    axios(servicePath.getListById + id).then(
      res => {
        resolve(res.data.data)
      }
    )
  })
  return {data: await promise}
}

export default MyList
